unit Rap_MalAlimArizaAnaliz;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB, wwdbdatetimepicker, ExtCtrls, Buttons,
  Grids, Wwdbigrd, Wwdbgrid, ppBands, ppClass, ppCtrls, ppVar, ppPrnabl,
  ppCache, ppDB, ppProd, ppReport, ppComm, ppRelatv, ppDBPipe,
  Mask, wwdbedit, Wwdotdot, TXComp, TXRB;

type
  TRap_MalAlimArizaAnalizF = class(TForm)
    RaporQ: TADOQuery;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    bastarihk: TwwDBDateTimePicker;
    bittarihk: TwwDBDateTimePicker;
    Label3: TLabel;
    Label4: TLabel;
    Panel2: TPanel;
    blistele: TBitBtn;
    bYazdir: TBitBtn;
    bkapat: TBitBtn;
    RaporQS: TDataSource;
    wwDBGrid1: TwwDBGrid;
    ppDBPipeline1: TppDBPipeline;
    ppReport1: TppReport;
    ppHeaderBand1: TppHeaderBand;
    ppDetailBand1: TppDetailBand;
    ppFooterBand1: TppFooterBand;
    ppTitleBand1: TppTitleBand;
    ppLabel1: TppLabel;
    ppSystemVariable1: TppSystemVariable;
    ppSystemVariable2: TppSystemVariable;
    ppDBText1: TppDBText;
    ppLabel2: TppLabel;
    ppDBText2: TppDBText;
    ppLabel3: TppLabel;
    ppDBText4: TppDBText;
    ppLabel5: TppLabel;
    ppDBText5: TppDBText;
    ppLabel6: TppLabel;
    ppDBText6: TppDBText;
    ppLabel7: TppLabel;
    ppDBText7: TppDBText;
    ppLabel8: TppLabel;
    ppLine1: TppLine;
    ppLine2: TppLine;
    ppGroup1: TppGroup;
    ppGroupHeaderBand1: TppGroupHeaderBand;
    ppGroupFooterBand1: TppGroupFooterBand;
    ppDBCalc1: TppDBCalc;
    ppDBCalc2: TppDBCalc;
    ppDBCalc3: TppDBCalc;
    ppDBCalc4: TppDBCalc;
    ppLine3: TppLine;
    ppSummaryBand1: TppSummaryBand;
    ppDBCalc5: TppDBCalc;
    ppDBCalc6: TppDBCalc;
    ppDBCalc7: TppDBCalc;
    ppDBCalc8: TppDBCalc;
    ppShape1: TppShape;
    ppLabel4: TppLabel;
    StokKoduk: TwwDBComboDlg;
    Label5: TLabel;
    FirmaAdik: TwwDBComboDlg;
    ppLabel9: TppLabel;
    ppDBText3: TppDBText;
    ppDBCalc9: TppDBCalc;
    ppDBCalc10: TppDBCalc;
    ExtraOptions1: TExtraOptions;
    procedure bkapatClick(Sender: TObject);
    procedure bYazdirClick(Sender: TObject);
    procedure blisteleClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure StokKodukKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure FirmaAdikExit(Sender: TObject);
    procedure FirmaAdikKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Rap_MalAlimArizaAnalizF: TRap_MalAlimArizaAnalizF;
  CariKod: integer;

implementation
uses data, UnitGenel;

{$R *.dfm}

procedure TRap_MalAlimArizaAnalizF.bkapatClick(Sender: TObject);
begin
  close;
end;

procedure TRap_MalAlimArizaAnalizF.bYazdirClick(Sender: TObject);
begin
  ppReport1.print;
end;

procedure TRap_MalAlimArizaAnalizF.blisteleClick(Sender: TObject);
begin
  RaporQ.close;
  RaporQ.sql.text := 'select stokkodu=dbo.getstokkodu(stokkod),tarih=convert(varchar(10),(select tarih from fatura f where f.bilisno=fathar.bilisno),104),';
  RaporQ.sql.add('bilisno,sum(miktar) as miktar,');
  RaporQ.sql.add('satilanadet=(select count(*) from fatharhar f where f.bilisnogir=fathar.bilisno and f.stokkod=fathar.stokkod and bilisnocik is not null),');
  RaporQ.sql.add('tumarizaliadet=(select count(*) from onarim o where o.serino in (select serinos from fatharhar f where f.bilisnogir=fathar.bilisno and f.stokkod=fathar.stokkod) and (select carikodgir from teknik where teknik.formno=o.formno)<>12810),');
  RaporQ.sql.add('degisenadet=(select count(*) from onarim o where o.serino in (select serinos from fatharhar f where f.bilisnogir=fathar.bilisno and f.stokkod=fathar.stokkod) and yserino is not null and (select carikodgir from teknik where teknik.formno=o.formno)<>12810)');
  RaporQ.sql.add('into #yeni from fathar where 1=1 ');

  if Stokkoduk.text <> '' then
    RaporQ.sql.add('and stokkod in (select stokkod from stok where stokkodu like ' + #39 + Stokkoduk.text + #39 + ')');

  if FirmaAdik.text <> '' then
    RaporQ.sql.add('and (select carikod from fatura f where f.bilisno=fathar.bilisno)=' + inttostr(CariKod));

  if bastarihk.Text <> '' then
    RaporQ.sql.add('and (select tarih from fatura f where f.bilisno=fathar.bilisno)>=' + #39 + bastarihk.Text + #39);

  if bittarihk.Text <> '' then
    RaporQ.sql.add('and (select tarih from fatura f where f.bilisno=fathar.bilisno)<=' + #39 + bittarihk.Text + #39);

  RaporQ.sql.add(' and (select faturatipi from fatura f where f.bilisno=fathar.bilisno)=1 and (select depokod from fatura f where f.bilisno=fathar.bilisno) in (1,2) ');
  RaporQ.sql.add('and exists(select serinos from fatharhar f where f.bilisnogir=fathar.bilisno) and ');
  RaporQ.sql.add('miktar>50');
  RaporQ.sql.add('group by bilisno,stokkod order by stokkodu,bilisno');
  RaporQ.sql.add('select *,arizaoran=(case satilanadet when 0 then 0 else cast(cast(tumarizaliadet as decimal)/cast(satilanadet as decimal) as decimal(10,3)) end) from #yeni');
  RaporQ.sql.add('select *,null as arizaoran from #yeni');
  RaporQ.sql.add('drop table #yeni');
  RaporQ.open;

  //memo1.lines:=Raporq.sql;
end;

procedure TRap_MalAlimArizaAnalizF.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := CaFree;
  Rap_MalAlimArizaAnalizF := nil;
end;

procedure TRap_MalAlimArizaAnalizF.StokKodukKeyPress(Sender: TObject;
  var Key: Char);
var
  Bilgi: string;
  StokKod: integer;
  Stokkodu: string[25];
begin
  if key = #13 then
  begin
    Bilgi := StokBul(Sender, 0, 'G');

    if Bilgi = '' then
      exit;

    if Bilgi <> '0' then
    begin
      Stokkod := Strtoint(Copy(Bilgi, 1, Pos(',', Bilgi) - 1));
      Delete(Bilgi, 1, Pos(',', Bilgi));
      Stokkodu := Copy(Bilgi, 1, Pos(',', Bilgi) - 1);

      Stokkoduk.text := Stokkodu;
    end
    else
    begin
      Stokkoduk.text := '';
    end;
  end;

end;

procedure TRap_MalAlimArizaAnalizF.FormShow(Sender: TObject);
begin
  bittarihk.date := SadeceTarih();
end;

procedure TRap_MalAlimArizaAnalizF.FirmaAdikExit(Sender: TObject);
var
  Bilgi: string;
  Carikodu: string[25];
  FirmaAdi: string[50];
begin
  Bilgi := CariBul(Sender);

  if Bilgi = '' then
    exit;

  if Bilgi <> '0' then
  begin
    Carikod := Strtoint(Copy(Bilgi, 1, Pos(',', Bilgi) - 1));
    Delete(Bilgi, 1, Pos(',', Bilgi));
    Carikodu := Copy(Bilgi, 1, Pos(',', Bilgi) - 1);
    Delete(Bilgi, 1, Pos(',', Bilgi));
    FirmaAdi := Bilgi;

    FirmaAdik.text := FirmaAdi;
  end
  else
  begin
    CariKod := 0;
    FirmaAdik.text := '';
  end;
end;

procedure TRap_MalAlimArizaAnalizF.FirmaAdikKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key = #13 then blistele.setfocus;
end;

end.

